# coding=utf-8
from email.mime.text import MIMEText  # 专门发送正文
from email.mime.multipart import MIMEMultipart  # 发送多个部分
from email.mime.application import MIMEApplication  # 发送附件
import smtplib  # 发送邮件

from datetime import datetime, date, timedelta

yesterday = (date.today() + timedelta(days = -1)).strftime("%m%d")    # 昨天日期
today = (date.today() + timedelta()).strftime("%m%d") # 今天日期

file = 'testfile%s.xlsx' % today  # 附件路径
print(file)
send_user = '675696416@qq.com'  # 发件人
password = ''  # 授权码/密码'675696416@qq.com'
receive_persons = ['675696416@qq.com', ''] # 收件人，可为list
cc_persons = ['675696416@qq.com', '']
subject = 'Python email test'  # 邮件主题
email_text = '邮件发送测试……'  # 邮件正文
server_address = 'smtp.qq.com'  # 服务器地址
mail_type = '1'  # 邮件类型

# 构造一个邮件体：正文 附件
msg = MIMEMultipart()
msg['Subject'] = subject  # 主题
msg['From'] = send_user  # 发件人
msg['To'] = ','.join(receive_persons)  # 收件人
msg['Cc'] = ','.join(cc_persons) #抄送人

# 构建正文
part_text = MIMEText(email_text)
msg.attach(part_text)  # 把正文加到邮件体里面去

# 构建邮件附件
# file = file           #获取文件路径
part_attach1 = MIMEApplication(open(file, 'rb').read())  # 打开附件
part_attach1.add_header('Content-Disposition', 'attachment', filename=('utf-8','',file))  # 为附件命名
msg.attach(part_attach1)  # 添加附件

# 发送邮件 SMTP
smtp = smtplib.SMTP(server_address, 25)  # 连接服务器，SMTP_SSL是安全传输

smtp.login(send_user, password)
smtp.sendmail(msg['From'], msg['To'].split(',')+ msg['Cc'].split(','),msg.as_string())  # 发送邮件
print('邮件发送成功！')